package com.io.test;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Date;

/**
 * Created by 41579 on 2016/12/21.
 */
public class testClass {
    public void fun1() throws IOException {
        PipedInputStream is = new PipedInputStream();
        PipedOutputStream os = new PipedOutputStream();
        is.connect(os);
        Thread t = new Thread(new myRunable(os));
        t.start();
//        byte[] bytes = new byte[1024];
//        while(is.read(bytes)>0){
//            System.out.println(new String(bytes));
//        }
    }

    public void fun2() throws Exception {
        FileInputStream fis = new FileInputStream("H:\\git\\knowledge\\IO_NIO\\file\\test.txt");
        FileOutputStream fos = new FileOutputStream("");
        byte[] bytes = new byte[1024];
        Thread t = new Thread(new myRunable2(fos));
        t.start();
        while (true) {
            System.out.println("....");
            fis.read(bytes);
            System.out.println("1" + new String(bytes));
        }
    }

    public void fun3() throws Exception {
        ByteOutputStream bos = new ByteOutputStream(1);
        while (true) {
            bos.write("test".getBytes());
            System.out.println("写完了");
        }
    }

    public void fun4() throws Exception {
        RandomAccessFile file = new RandomAccessFile("H:\\git\\knowledge\\IO_NIO\\file\\test.txt", "rw");
        FileChannel fileChannel = file.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        //       byteBuffer.put("hello".getBytes());
//        byteBuffer.flip();
//        while(true) {
        //写模式
        fileChannel.read(byteBuffer);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.capacity());
        System.out.println(byteBuffer.limit());
        //读模式
        byteBuffer.flip();
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.capacity());
        System.out.println(byteBuffer.limit());
//        }
//        byteBuffer.flip();
//        Thread.sleep(3000);
//        Thread t = new Thread(new Runnable() {
//            public void run() {
//                try {
//                    ByteBuffer byteBuffer2 = ByteBuffer.allocate(1024);
//                    RandomAccessFile file2 = new RandomAccessFile("H:\\git\\knowledge\\IO_NIO\\file\\test.txt", "rw");
//                    FileChannel fileChannel2 = file2.getChannel();
//                    fileChannel.read(byteBuffer2);
//                    byteBuffer2.flip();
//                    System.out.println(byteBuffer2.get());
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//        t.start();
    }

    public void fun5() throws Exception {
        RandomAccessFile file = new RandomAccessFile("H:\\git\\knowledge\\IO_NIO\\file\\test.txt", "rw");
        RandomAccessFile file2 = new RandomAccessFile("H:\\git\\knowledge\\IO_NIO\\file\\test2.txt", "rw");
        FileChannel fileChannel = file.getChannel();
        FileChannel fileChannel2 = file2.getChannel();
        fileChannel2.transferFrom(fileChannel,0,fileChannel.size());
    }


    class myRunable2 implements Runnable {
        protected FileOutputStream os;

        public myRunable2(FileOutputStream os) {
            this.os = os;
        }

        public void run() {
            while (true) {
                try {
                    os.write(("hello" + new Date()).toString().getBytes());
                    System.out.println("写完了。。。");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class myRunable implements Runnable {
        protected PipedOutputStream os;

        public myRunable(PipedOutputStream os) {
            this.os = os;
        }

        public void run() {
            while (true) {
                try {
                    Thread.sleep(10000);
                    os.write(("hello" + new Date()).toString().getBytes());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
